<?php

namespace Apps\Weixin\Services;


use Apps\Common\Service\BaseService;
use Deyi\Functions\HttpFunction;

class WxMessageService extends BaseService
{
    public function sendKfMsg($appId, $openId, $msg, $accessToken = '')
    {
        $url = sprintf(
            'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s',
            $accessToken ? $accessToken : WxResourceService::getInstance()->getAuthorizerAccessToken($appId)
        );

        $ret = HttpFunction::postDataToUrl($url, json_encode([
            'touser' => $openId,
            'msgtype' => 'text',
            'text' => [
                'content' => $msg
            ]
        ], JSON_UNESCAPED_UNICODE));

        if ($ret && ($json = json_decode($ret)) && !$json->errcode) {
            return true;
        }

        static::$_logger->info(' send kf msg fail:' . $ret . ' ' . $accessToken . ' ' . $openId);

        return false;
    }
}